package cn.iune.erp.customer;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;
import com.justep.baas.data.Row;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;

@Transactional
@Service
public class FileCustomerGroupServiceImpl implements IFileCustomerGroupService {

	@Autowired
	private IFileCustomerGroupDao dao;
	
	
	@Override
	public List<FileCustomerGroup> getAll(){
		return dao.getAll();
	}
	
	@Override
	public List<FileCustomerGroup> getList(String enterpriseNo, Integer offset, Integer limit) {
		return dao.getList(enterpriseNo, offset, limit);
	}
	
	
	@Override
	public JSONObject querySimple(JSONObject param){
		
		JSONObject json = new JSONObject();
		JSONObject data;
		try {
			data = dao.querySimple(param);
		} catch (SQLException e) {
			e.printStackTrace();
			
			json.put("code", -1);
			json.put("status", e.getMessage());
			json.put("msg", "failure");
			
			return json;
		}
		//开始装配
		
		JSONArray arrayItem = new JSONArray();
		
		JSONArray rows = data.getJSONArray("data");
		
		for(int i =0;i<rows.size();i++) {
			
			//转换成JSON
			JSONObject rowObject = rows.getJSONObject(i);
			/*
			//装配图片
			//String itemName = rowObject.getString("itemName");
			//log.info("querySimple.itemName-->>"+itemName);
			String imageurl = rowObject.getString("imageurl");
			//log.info("querySimple.imageurl-->>"+imageurl);
			JSONArray arrayImageurl = new JSONArray();
			if(imageurl!=null) {
				//单图
				//http://www.iune.cn/group1/M00/00/00/cxzWEVq2MjCAc8JsAAAuxlb_9jI617.jpg
				String ossServer = appConfigParams.getOssServer();//"http://www.iune.cn";
				arrayImageurl = JSONArray.parseArray(imageurl);
				//[{"remoteFileName":"M00/00/01/cxzWEVuxsaGAGM53AABkeWyTCEw855.jpg","groupName":"group1","originalFilename":"131097612878657419_wmk.jpg"},{"remoteFileName":"M00/00/01/cxzWEVtZ24yAMd2MAACxW7HAcco856.jpg","groupName":"group1","originalFilename":"589d3de5N48cfbe46.jpg"},{"remoteFileName":"M00/00/01/cxzWEVtZVsKAEKl3AAAcaGOOVFo105.jpg","groupName":"group1","originalFilename":"ljdxm.jpg"},{"remoteFileName":"M00/00/00/cxzWEVtVQp-AMgkoAAAVTb55arQ384.png","groupName":"group1","originalFilename":"Untitled.png"}]
				//log.info(arrayImageurl.toJSONString());
				JSONObject jsonObject = arrayImageurl.getJSONObject(0);
				String groupName = jsonObject.getString("groupName");
				String remoteFileName = jsonObject.getString("remoteFileName");
				String url = ossServer + "/" + groupName + "/" + remoteFileName;
				//封面（单图）
				rowObject.put("pic", url);
			}
			*/
			
			arrayItem.add(rowObject);
		}
		
		json.put("data", arrayItem);
		json.put("code", 0);
		json.put("msg", "success");
		json.put("total", data.getBigInteger("total"));

		return json;
	}

		
	@Override
	public FileCustomerGroup getByID(String fID) {
		return dao.getByID(fID);
	}

	@Transactional
	@Override
	public void delete(String fID) {
		dao.delete(fID);
	}

	@Transactional
	@Override
	public JSONObject save(JSONObject object) throws ParseException {
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
		
		JSONObject data = new JSONObject();
		data.put("concept", table.getTableName());
//		JSONArray rows = new JSONArray();
//		List<String> idsNew = new ArrayList<String>();
//		List<String> idsDel = new ArrayList<String>();
				
		List<Row> list = table.getRows();
		for(Row row : list){
			JSONObject javaObject= Transform.rowToJavaObject(table, row);
			FileCustomerGroup obj = JSON.toJavaObject(javaObject, FileCustomerGroup.class);
			if(RowState.NEW.equals(obj.getRowState())){
				dao.insert(obj);
//				idsNew.add(obj.getfID());
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				//由于前端已经将删除的子节点放进删除缓冲区，所以无需下面这段代码特殊处理
				/*
				//删除树形子节点
				List<FileCustomerGroup> objs = this.getChildren(obj.getfID());
				for(FileCustomerGroup o :objs){
					dao.delete(o.getfID());
				}
				*/
				//删除自身
				dao.delete(obj.getfID());
//				idsDel.add(obj.getfID());
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				dao.update(obj);
			}
		}
		
		data.put("code", 0);
		data.put("msg", "success");
		return data;
	}
	

	
	///////////////////////业务功能///////////////////////////////////////
	@Override
	public List<String> getRootIDs(String enterpriseNo) {
		// TODO Auto-generated method stub
		List<String> ids = new ArrayList<String>();
		List<FileCustomerGroup> objs = dao.getRoots(enterpriseNo);
		for(FileCustomerGroup obj : objs){
			ids.add(obj.getfID());
		}
		return ids;
	}

	
	@Override
	public List<String> getChildIDs(String fParent) {
		// TODO Auto-generated method stub
		List<String> ids = new ArrayList<String>();
		List<FileCustomerGroup> objs = dao.getChilds(fParent);
		for(FileCustomerGroup obj : objs){
			ids.add(obj.getfID());
		}
		return ids;
	}

	//更新修改人
	@Transactional
	@Override
	public int updateAlterUser(FileCustomerGroup fs, String tUsercode, String tUsername) {
		
		fs.settUsercode(tUsercode);
		fs.settUsername(tUsername);
		fs.settDatetime(Utils.getCurrentDateTime());
		
		dao.update(fs);
		
		return 0;
	}

	
	

	
	
	//更新层级
	@Transactional
	@Override
	public int updateNodeLevel(FileCustomerGroup fs,int nodeLevel) {
		fs.setNodeLevel(nodeLevel);
		dao.update(fs);
		return 0;
	}
	//更新层级
	@Transactional
	@Override
	public int updateILevel(String rowID, int nodeLevelParent){
		//彻底重新排序
		
		FileCustomerGroup fs = dao.getByID(rowID);
		
		//首先更新本级层级
		int updateResult = 0;
		updateResult = this.updateNodeLevel(fs,nodeLevelParent + 1);
		if (updateResult!=0){
			return -1;
		}
		
		//2、判断是否有下一级
		String nodeKind = fs.getNodeKind();
		if (!"nkLeaf".equals(nodeKind)){
			//不是叶子，需要处理下一级（递归调用即可）
			//递归调用，处理下级。
			List<String> idsChild = this.getIDsByParent(rowID,fs.getEnterpriseNo());
			for(Iterator<String> i = idsChild.iterator();i.hasNext();){
				String strID = (String)i.next();
//				FileCustomerGroup groupInfoChild = new FileCustomerGroup(strID);
				updateResult = this.updateILevel(strID,nodeLevelParent + 1 );
				if (updateResult!=0){
					return -1;
				}
			}
		}
		return 0;
	}
	//更新叶子状态
	@Transactional
	@Override
	public int updateNodeKind(FileCustomerGroup fs,String nodeKind) {
		fs.setNodeKind(nodeKind);
		dao.update(fs);
		return 0;
	}
	//更新父级
	@Transactional
	@Override
	public int updateParentID(FileCustomerGroup fs,String fParent) {
		fs.setfParent(fParent);
		dao.update(fs);
		return 0;
	}

	//更新sortNo
	@Transactional
	@Override
	public int updateSortNo(FileCustomerGroup fs,String sortNo) {
		fs.setSortNo(sortNo);
		dao.update(fs);
		return 0;
	}
	//更新全路径ID
	@Transactional
	@Override
	public int updateFullID(FileCustomerGroup fs,String groupFullID) {
		fs.setGroupFullID(groupFullID);
		dao.update(fs);
		return 0;
	}
	
	//更新全路径名称
	@Transactional
	@Override
	public int updateFullLabel(FileCustomerGroup fs,String groupFullLabel) {
		fs.setGroupFullLabel(groupFullLabel);
		dao.update(fs);
		return 0;
	}
	
	//更新全路径ID和名称
	@Transactional
	@Override
	public int updateFullNoLabel(FileCustomerGroup fs,String groupFullNo,String groupFullLabel) {
//		fs.setClassFullNo(groupFullNo);
		fs.setGroupFullLabel(groupFullLabel);
		dao.update(fs);
		return 0;
	}
	
	//获取子节点数量
	@Override
	public int getChildCount(String fParent) {
		if (fParent==null || "".equals(fParent)){
			//System.out.println("dao.getRootCount()-->"+dao.getRootCount());
			return dao.getRootCount();
		}else{
			return dao.getChildCount(fParent);
		}
	}
	
	//新增行的排序号编写
	@Override
	public String getSortNoByParent(String fParent) {
		//System.out.println("fParent-->"+fParent);
		//排序号分为两部分：父级序号+新增本级序号
		//1、父级序号
		String sortNoParent = "";
		String sortNoChild = "";
		
		//2、新增本级序号

		if (fParent!=null && !fParent.equals("")){
			FileCustomerGroup groupInfo = dao.getByID(fParent);//new FileCustomerGroup(fParent);
			sortNoParent = groupInfo.getSortNo();
		}
		
		//2、获取子节点数量
//		FileCustomerGroup groupInfoChild = new FileCustomerGroup();
		int count = this.getChildCount(fParent);
		//System.out.println("count-->"+count);
		count = count + 1;
		sortNoChild = "/" + String.format("%03d", count);

		return (sortNoParent+sortNoChild);
	}

	//获取ids
	@Override
	public List<String> getIDsByParent(String fParent, String enterpriseNo){
		if (fParent!=null && !fParent.equals("")){
			return this.getChildIDs(fParent);
		}else{
			return this.getRootIDs(enterpriseNo);
		}
	}
	
	
	//更新全路径信息
	@Transactional
	@Override
	public int rewriteFullInfoByFullID(String fID,String groupFullID,String nodeKind/*,ModifyState rowState*/) {
		
		//1、首先处理本级
		//解析全路径，配置全编码和全名称（暂时不考虑子数据）
		//int stringLen = 32;
		int stringCount = groupFullID.length();///(1+stringLen);
		if (stringCount!=0){
			String groupFullNo = "";
			String groupFullLabel = "";
			/*
			//方法一。定长
			///////////////////////////////解析全路径字符串"/dkj/xxdjxx/yyy/kk"///////////////////////
			//System.out.println("需处理级数stringCount："+stringCount);
			for (int index = 0;index<stringCount;index++){
				//System.out.println("正在处理级数index："+index);
				String singleID = groupFullID.substring(index*stringLen+(index+1), index*stringLen+stringLen+(index+1));
				//System.out.println(index+"级ID-->"+singleID+"-->ID长度："+singleID.length());
				FileCustomerGroup groupInfo = new FileCustomerGroup(singleID);
				//System.out.println(index+"级ID-->"+singleID+"-->IDLabel："+groupInfo.getClassLabel());
				
				//累加分组代码和分组名称，即全路径代码和名称
				groupFullNo = groupFullNo + "/" + groupInfo.getClassNo();
				groupFullLabel = groupFullLabel + "/" + groupInfo.getClassLabel();
				
			}
			//////////////////////////////////////////////////////////////////////////////////////////
			*/
			//方法二。变长
			///////////////////////////////解析全路径字符串"/dkj/xxdjxx/yyy/kk"///////////////////////
			String[] groupIDs  = groupFullID.split("/");
			for(String groupID:groupIDs){
				//System.out.println("---------------------------------------------groupID-->"+groupID);
				if(groupID!=null && !groupID.equals("")){
					FileCustomerGroup groupInfo = dao.getByID(groupID);//new FileCustomerGroup(groupID);
					//System.out.println(index+"级ID-->"+singleID+"-->IDLabel："+groupInfo.getClassLabel());
					
					//累加分组代码和分组名称，即全路径代码和名称
//					groupFullNo = groupFullNo + "/" + groupInfo.getClassNo();
					groupFullLabel = groupFullLabel + "/" + groupInfo.getGroupLabel();
				}
			}
			//////////////////////////////////////////////////////////////////////////////////////////
			
			//将全路径代码和名称写入数据库
			//row.setString("groupFullNo", groupFullNo);
			//row.setString("groupFullLabel", groupFullLabel);
			
			int updateResult = 0;
			FileCustomerGroup groupInfo = dao.getByID(fID);//new FileCustomerGroup(fID);
			if (!groupFullID.equals(groupInfo.getGroupFullID())){
				updateResult = this.updateFullID(groupInfo,groupFullID);
				if (updateResult!=0){
					return -1;
				}
			}
			
			groupInfo = dao.getByID(fID);
			updateResult = this.updateFullNoLabel(groupInfo,groupFullNo, groupFullLabel);
			if (updateResult!=0){
				return -1;
			}
			/*
			//如果是新增的数据，则处理排序号
			if (ModifyState.NEW.equals(rowState)){
				//增加（前台数据有效）
				
			}
			*/
			//2、判断是否有下一级
			if (!"nkLeaf".equals(nodeKind)){
				//不是叶子，需要处理下一级（递归调用即可）
				//1、取出儿子列表（需具备的数据fID、FullID和nodeKind）
				List<FileCustomerGroup> fss= dao.getChilds(fID);
				for(FileCustomerGroup fs : fss){
					String fIDChild=fs.getfID();
					String nodeKindChild=fs.getNodeKind();
					updateResult = this.rewriteFullInfoByFullID(
							fIDChild
							, /*groupFullIDChild*/groupFullID + "/" + fIDChild
							, nodeKindChild);
					if (updateResult!=0){
						return -1;
					}
					
				}
				/*
				String checkOutSql= "SELECT FileCustomerGroup as fID"
					+ ",FileCustomerGroup.groupFullID as groupFullID"
					+ ",FileCustomerGroup.nodeKind as nodeKind"

					+ " FROM FileCustomerGroup FileCustomerGroup " 
					+ " WHERE ( FileCustomerGroup.fParent = :fParent ) ";
		        //给变量赋值
				Map<String, Object> valuesMap=new HashMap<String, Object>(); 
		        valuesMap.put("fParent",fID);

				Table table = KSQL.select(checkOutSql, valuesMap, "/inventory/bStorePick/data", null);
				Iterator<Row> rows=null;
				Row row=null;
				rows = table.iterator();
				while (rows.hasNext()) {
					row = rows.next();
					String fIDChild=row.getString("fID");
					String nodeKindChild=row.getString("nodeKind");
					updateResult = this.rewriteFullInfoByFullID(
							fIDChild
							, groupFullIDChildgroupFullID + "/" + fIDChild
							, nodeKindChild);
					if (updateResult!=0){
						return -1;
					}
				}
				*/
				
			}
			
		}
		return 0;
	}
	
	//移动
	@Transactional
	@Override
	public int move(String rowID,String oldParentID,String newParentID){
		//处理的重要原则：按照先删除，再插入的原则
		//移动操作分为两部分：
		//1、更新本级的fParent字段、gourpFullID字段、groupFullNo字段、gourpFullLabel字段和sortNo字段、nodeLevel字段；
		//2、更新下级的同样信息
		int updateResult = 0;
		
		//更新本级前，保留新父排序号
//		FileCustomerGroup groupInfoSort = new FileCustomerGroup();
		String newSortNo = this.getSortNoByParent(newParentID);

		//2、更新本级fParent
		FileCustomerGroup groupInfo = dao.getByID(rowID);//new FileCustomerGroup(rowID);
		groupInfo.setfParent(newParentID);
		updateResult = this.updateParentID(groupInfo,newParentID);
		if (updateResult!=0){
			return -1;
		}
		
		//更新排序（如果包含子级，则需处理）
		groupInfo = dao.getByID(rowID);
		updateResult = this.updateSortNo(groupInfo,newSortNo);
		if (updateResult!=0){
			return -1;
		}
		//判断是否有下一级（需排序）
		String nodeKind = groupInfo.getNodeKind();
		if (!"nkLeaf".equals(nodeKind)){
			//不是叶子，需要处理下一级排序
			List<String> idsChild = this.getIDsByParent(rowID,groupInfo.getEnterpriseNo());
//			List<Integer> versionsChild = groupInfo.getVersionsByParent(rowID);
//			FileCustomerGroup groupInfoChild = new FileCustomerGroup();
			updateResult = this.sort(rowID,idsChild /*,versionsChild*/ );
			if (updateResult!=0){
				return -1;
			}
		}		
		
		//1、原父节点末端状态更改
		if(oldParentID!=null){
			FileCustomerGroup groupInfoParentOld = dao.getByID(oldParentID);//new FileCustomerGroup(oldParentID);
			if (this.getChildCount(oldParentID)==0){
				groupInfoParentOld.setNodeKind("nkLeaf");
				updateResult = this.updateNodeKind(groupInfoParentOld,"nkLeaf");
				if (updateResult!=0){
					return -1;
				}
			}else{
				groupInfoParentOld = dao.getByID(oldParentID);
				groupInfoParentOld.setNodeKind(null);//////////////////////////////////////////////////////////////////////////
				updateResult = this.updateNodeKind(groupInfoParentOld,null);
				if (updateResult!=0){
					return -1;
				}
			}
			
		}
		
		//1.1原父节点重新排序
		List<String> idsChild = this.getIDsByParent(oldParentID,groupInfo.getEnterpriseNo());
//		List<Integer> versionsChild = groupInfo.getVersionsByParent(oldParentID);
//		FileCustomerGroup groupInfoChild = new FileCustomerGroup();
		updateResult = this.sort(oldParentID,idsChild /*,versionsChild*/ );
		if (updateResult!=0){
			return -1;
		}
		

		//3、新父节点末端状态更改
		if(newParentID!=null){
			FileCustomerGroup groupInfoParentNew = dao.getByID(newParentID);//new FileCustomerGroup(newParentID);
			if (this.getChildCount(newParentID)==0){
				groupInfoParentNew.setNodeKind("nkLeaf");
				updateResult = this.updateNodeKind(groupInfoParentNew,"nkLeaf");
				if (updateResult!=0){
					return -1;
				}
			}else{
				groupInfoParentNew.setNodeKind(null);
				updateResult = this.updateNodeKind(groupInfoParentNew,null);
				if (updateResult!=0){
					return -1;
				}
			}
			
		}
		
		//更新全路径信息
		String fullID = "";
		if (newParentID==null){
			fullID = "/"+rowID;
		}else{
			FileCustomerGroup groupInfoParentNew = dao.getByID(newParentID);//new FileCustomerGroup(newParentID);
			fullID = groupInfoParentNew.getGroupFullID()+"/"+rowID;
		}
//		FileCustomerGroup groupInfoFull = new FileCustomerGroup();
		updateResult = this.rewriteFullInfoByFullID(rowID, fullID, groupInfo.getNodeKind());
		if (updateResult!=0){
			return -1;
		}
		
		//更新层级
		if (newParentID==null){
			updateResult = this.updateILevel(rowID, 0);
//		if (groupInfoParentNew.getNodeLevel()!=groupInfoParentOld.getNodeLevel()){
		}else{
//			FileCustomerGroup groupInfoLevel = new FileCustomerGroup();
			FileCustomerGroup groupInfoParentNew = dao.getByID(newParentID);//new FileCustomerGroup(newParentID);
			updateResult = this.updateILevel(rowID, groupInfoParentNew.getNodeLevel());
			if (updateResult!=0){
				return -1;
			}
		}
		
		return 0;
	}
	
	
	//排序
	@Transactional
	@Override
	public int sort(String parentID,List<String> ids){
		//彻底重新排序
		
		//首先获取父级排序号
		String fParent = parentID;
		String sortNoParent = "";
		if (fParent!=null && !fParent.equals("")){
			FileCustomerGroup groupInfo = dao.getByID(fParent);//new FileCustomerGroup(fParent);
			sortNoParent = groupInfo.getSortNo();
		}
		
		//获取传入的新排序的列表
		int index = 0;
		for(Iterator<String> i = ids.iterator();i.hasNext();){
			String strID = (String)i.next();
			index++;
			//新编写排序号
			//System.out.println("ids."+strID);
			String sortNoChild = "/" + String.format("%03d", index);
			String sortNoNew = sortNoParent + sortNoChild;
			FileCustomerGroup groupInfo = dao.getByID(strID);//new FileCustomerGroup(strID);
			if (!sortNoNew.equals(groupInfo.getSortNo())){
				//需要更新新序号
				//System.out.println("序号不同，需写入数据库！");
				int updateResult = this.updateSortNo(groupInfo,sortNoNew);
				if (updateResult!=0){
					return -1;
				}
				
				//2、判断是否有下一级
				String nodeKind = groupInfo.getNodeKind();
				if (!"nkLeaf".equals(nodeKind)){
					//不是叶子，需要处理下一级（递归调用即可）
					//递归调用，处理下级。
					List<String> idsChild = this.getIDsByParent(strID,groupInfo.getEnterpriseNo());
//					List<Integer> versionsChild = groupInfo.getVersionsByParent(fParent);
//					FileCustomerGroup groupInfoChild = new FileCustomerGroup();
					updateResult = this.sort(strID,idsChild /*,versionsChild*/ );
					if (updateResult!=0){
						return -1;
					}
				}
			}
			//System.out.println("旧序号." + groupInfo.getSortNo());
			//System.out.println("新序号." + sortNoNew);
			
		}
		/*
		for(Iterator<Integer> i = versions.iterator();i.hasNext();){
			int iVersion = i.next();
			System.out.println("versions."+iVersion);
		}
		*/
		return 0;
	}
	
	@Transactional
	@Override
	public int sort(String parentID, JSONArray jsonArray){
		List<String> ids = new ArrayList<String>();
		for(int i=0; i<jsonArray.size();i++){
			ids.add(jsonArray.getString(i));
		}

		return this.sort(parentID, ids);
	}
	
	//获取数据库的父
	@Override
	public FileCustomerGroup getParent(FileCustomerGroup fs){
		if(null==fs.getfParent()){
			return null;
		}else{
//			FileCustomerGroup itemClass = new FileCustomerGroup(this.getfParent());
			return dao.getByID(fs.getfParent());
		}
	}
		
	//获取数据库的根
	@Override
	public FileCustomerGroup getRoot(FileCustomerGroup fs){
		FileCustomerGroup itemClass = fs;
		FileCustomerGroup parent = this.getParent(fs);
		while(parent!=null){
			itemClass = parent;
			parent = this.getParent(parent);
		}
		
		//System.out.println("itemClass"+itemClass.getfID());
		return itemClass;
	}
	
	
	@Override
	public List<FileCustomerGroup> getChildren(String fParent) {
		// TODO Auto-generated method stub
		return dao.getChildren(fParent);
	}
	
	@Override
	public JSONObject newData(JSONObject data) {
		return dao.create(data);
	}
	
	@Override
	public JSONObject deleteData(JSONObject param) {
		//System.out.println("fileFactoryCreate.param-->>"+param.toJSONString());
		JSONArray ids = param.getJSONArray("ids");
		for(int i=0;i<ids.size();i++){
			String id = ids.getString(i);
			//这里可以增加是否可删除的判断
			//删除树形子节点
			List<FileCustomerGroup> objs = this.getChildren(id);
			for(FileCustomerGroup o :objs){
				dao.delete(o.getfID());
			}
			
			dao.delete(id);
		}

		JSONObject data = new JSONObject();
		data.put("ids", ids);
		
		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("msg", "success");
		json.put("data", data);
		
		return json;
	}

	/*
	在@Transaction注解中定义noRollbackFor和RollbackFor指定某种异常是否回滚。 
	@Transactional(noRollbackFor=RuntimeException.class) 
	@Transactional(RollbackFor=Exception.class) 
	*/
	@Transactional
	@Override
	public JSONObject saveData(JSONObject param) {
		//Spring的事务管理默认只对出现运行期异常(java.lang.RuntimeException及其子类)进行回滚。 
		//如果一个方法抛出Exception或者Checked异常，Spring事务管理默认不进行回滚。 
		// JSON转换Table
		//System.out.println(object);
		JSONObject data;
		try {
			data = this.save(param);
					
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", data);
			
			return json;
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("msg", e.getMessage());
			
			return json;
		} 
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public JSONObject queryData(JSONObject param){
		//在此层面（ServiceImpl）统一处理异常，此层面直接被controller调用，面对BizData
		
		try {
			JSONObject jsonTable = dao.query(param);
			
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", jsonTable);
			
			return json;
		} catch (SQLException e) {
			e.printStackTrace();
			
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("msg", e.getMessage());
			return json;
		}
	}
	
	@Override
	public JSONObject validData(JSONObject param) {
		// TODO Auto-generated method stub
		return null;
	}



}
